/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2010 huliqing, huliqing.cn@gmail.com
 *
 * This file is part of QFaces.
 * QFaces is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QFaces is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with QFaces.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 这个文件是QFaces的一部分。
 * 您可以单独使用或分发这个文件，但请不要移除这个头部声明信息.
 * QFaces是一个自由软件，您可以自由分发、修改其中的源代码或者重新发布它，
 * 新的任何修改后的重新发布版必须同样在遵守LGPL3或更后续的版本协议下发布.
 * 关于LGPL协议的细则请参考COPYING、COPYING.LESSER文件，
 * 您可以在QFaces的相关目录中获得LGPL协议的副本，
 * 如果没有找到，请连接到 http://www.gnu.org/licenses/ 查看并获取副本。
 *
 * - Author: Huliqing
 * - Contact: huliqing.cn@gmail.com
 * - License: GNU Lesser General Public License (LGPL)
 * - Blog and source code availability: http://www.huliqing.name/
 */

package name.huliqing.qfaces.render;

import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

import name.huliqing.qfaces.QFaces;
import name.huliqing.qfaces.component.Frame;
import name.huliqing.qfaces.component.Frame.CssPosition;

public class FrameRenderer extends Renderer{

	private final static String STYLE_FIX_BUG="_qf_non";
	private Frame f;
	
	@Override
	public void encodeBegin(FacesContext fc, UIComponent ui)
			throws IOException {
		super.encodeBegin(fc, ui);
		this.f = (Frame) ui;
		
		if (f.getFrameId() == null)
			f.setFrameId(f.getClientId(fc));
		
		if (f.getHeaderId() == null)
			f.setHeaderId(f.getFrameId() + ":-header");
		
		if (f.getCloseId() == null)
			f.setCloseId(f.getFrameId() + ":-close");
		
		if (f.getLabelHeader() == null)
			f.setLabelHeader(f.getClass().getSimpleName());
		
		if (f.getVisible() == null)
			f.setVisible(Boolean.TRUE);
		
		if (f.getDrag() == null)
			f.setDrag(Boolean.FALSE);
		
		if (f.getRenderBorder() == null)
			f.setRenderBorder(Boolean.TRUE);
		
		if (f.getRenderCloseButton() == null)
			f.setRenderCloseButton(Boolean.TRUE);
		
		if (f.getStyleClassHeader() == null)
			f.setStyleClassHeader("_qf_07_header");
		
		if (f.getCssPosition() == null)
			f.setCssPosition(CssPosition.STATIC.name());
		
		if (f.getFace() == null)
			f.setFace("1");
		
		if (f.getRenderBorder()) {
            this.encodeBeginWidthBorder(fc.getResponseWriter());
        } else {
            this.encodeBeginNoBorder(fc.getResponseWriter());
        }
	}

	@Override
	public void encodeEnd(FacesContext fc, UIComponent ui)
			throws IOException {
		super.encodeEnd(fc, ui);
		
		if (f.getRenderBorder()) {
            this.encodeEndWidthBorder(fc.getResponseWriter());
        } else {
            this.encodeEndNoBorder(fc.getResponseWriter());
        }

        if (f.getDrag()) {
            String dragEvent = "Q.U.toEnableDrag(\"" + f.getHeaderId() + "\", \"" + f.getFrameId() + "\");";
            QFaces.getResLoader2().loadJavaScriptInit(f, dragEvent);
        }
        if (f.getVisible()) {
            // 组件默认初始状态都是display:none, 只有在相关js装载完毕之后才打开。
            String visibleEvent = "Q.U.open(\"" + f.getFrameId() + "\")";
            QFaces.getResLoader2().loadJavaScriptInit(f, visibleEvent);
        }
	}


	private final void encodeBeginWidthBorder(ResponseWriter rw) throws IOException {
        String FRAME_00 = "07-" + f.getFace() + "-00.png";
        String FRAME_01 = "07-" + f.getFace() + "-01.png";
        String FRAME_02 = "07-" + f.getFace() + "-02.png";
        String FRAME_10 = "07-" + f.getFace() + "-10.png";
        String FRAME_11 = "07-" + f.getFace() + "-11.png";
        String FRAME_CLOSE = "07-" + f.getFace() + "-close.png";

        rw.startElement("table", f);
        rw.writeAttribute("id", f.getFrameId(), null);
        rw.writeAttribute("style", "display:none;position:" + f.getCssPosition(), null);
        rw.writeAttribute("border", "0", null);
        rw.writeAttribute("cellspacing", "0", null);
        rw.writeAttribute("cellpadding", "0", null);
        rw.startElement("tr", f);
        rw.startElement("td", f);
        rw.writeAttribute("class", STYLE_FIX_BUG, null);
        QFaces.getResLoader2().loadImage(FRAME_00);
        rw.endElement("td");

        rw.startElement("td", f);
        rw.writeAttribute("style", "background:url(" + QFaces.getResLoader2().makeImageSrc(FRAME_01) + ") repeat-x", null);
        rw.startElement("table", f);
        rw.writeAttribute("class", f.getStyleClassHeader(), null);
        rw.writeAttribute("border", "0", null);
        rw.writeAttribute("cellspacing", "0", null);
        rw.writeAttribute("cellpadding", "0", null);
        rw.writeAttribute("width", "100%", null);
        rw.startElement("tr", f);
        rw.startElement("td", f);
        rw.writeAttribute("id", f.getHeaderId(), null);
        rw.writeText(f.getLabelHeader(), null);
        rw.endElement("td");
        rw.startElement("td", f);
        rw.writeAttribute("width", "1", null);
        if (f.getRenderCloseButton()) {
            rw.startElement("img", f);
            rw.writeAttribute("id", f.getCloseId(), null);
            rw.writeAttribute("src", QFaces.getResLoader2().makeImageSrc(FRAME_CLOSE), null);
            rw.writeAttribute("style", "cursor:pointer;", null);
            rw.writeAttribute("onclick", "Q.U.close(\"" + f.getFrameId() + "\")", null);
            rw.endElement("img");
        }
        rw.endElement("td");
        rw.endElement("tr");
        rw.endElement("table");
        rw.endElement("td");

        rw.startElement("td", f);
        rw.writeAttribute("class", STYLE_FIX_BUG, null);
        QFaces.getResLoader2().loadImage(FRAME_02);
        rw.endElement("td");
        rw.endElement("tr");

        rw.startElement("tr", f);
        rw.startElement("td", f);
        rw.writeAttribute("class", STYLE_FIX_BUG, null);
        rw.writeAttribute("style", "background:url(" + QFaces.getResLoader2().makeImageSrc(FRAME_10) + ") repeat-y", null);
        rw.endElement("td");

        rw.startElement("td", f);
        rw.writeAttribute("style", "background-image:url(" + QFaces.getResLoader2().makeImageSrc(FRAME_11) + ")", null);
    }

    private final void encodeEndWidthBorder(ResponseWriter rw) throws IOException {
        String FRAME_12 = "07-" + f.getFace() + "-12.png";
        String FRAME_20 = "07-" + f.getFace() + "-20.png";
        String FRAME_21 = "07-" + f.getFace() + "-21.png";
        String FRAME_22 = "07-" + f.getFace() + "-22.png";

        rw.endElement("td");
        rw.startElement("td", f);
        rw.writeAttribute("class", STYLE_FIX_BUG, null);
        rw.writeAttribute("style", "background:url(" + QFaces.getResLoader2().makeImageSrc(FRAME_12) + ") repeat-y", null);
        rw.endElement("td");
        rw.endElement("tr");
        rw.startElement("tr", f);
        rw.startElement("td", f);
        rw.writeAttribute("class", STYLE_FIX_BUG, null);
        QFaces.getResLoader2().loadImage(FRAME_20);
        rw.endElement("td");

        rw.startElement("td", f);
        rw.writeAttribute("class", STYLE_FIX_BUG, null);
        rw.writeAttribute("style", "background:url(" + QFaces.getResLoader2().makeImageSrc(FRAME_21) + ") repeat-x", null);
        rw.endElement("td");

        rw.startElement("td", f);
        rw.writeAttribute("class", STYLE_FIX_BUG, null);
        QFaces.getResLoader2().loadImage(FRAME_22);
        rw.endElement("td");
        rw.endElement("tr");
        rw.endElement("table");
    }

    private final void encodeBeginNoBorder(ResponseWriter rw) throws IOException {
        rw.startElement("table", f);
        rw.writeAttribute("id", f.getFrameId(), null);
        rw.writeAttribute("style", "display:none;position:" + f.getCssPosition(), null);
        rw.writeAttribute("border", "0", null);
        rw.writeAttribute("cellspacing", "0", null);
        rw.writeAttribute("cellpadding", "0", null);
        rw.startElement("tr", f);
        rw.startElement("td", f);
    }

    private final void encodeEndNoBorder(ResponseWriter rw) throws IOException {
        rw.endElement("td");
        rw.endElement("tr");
        rw.endElement("table");
    }
}
